package com.eagle.platform.api.controller;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.eagle.common.entity.po.AdminRoles;
import com.eagle.common.entity.po.UserRRoles;
import com.eagle.common.entity.po.query.AdminRolesQuery;
import com.eagle.common.entity.po.query.AdminUserQuery;
import com.eagle.common.entity.po.query.UserRRolesQuery;
import com.eagle.common.entity.vo.PaginationResult;
import com.eagle.common.exception.BusinessException;
import com.eagle.common.service.UserRRolesService;
import com.eagle.common.util.JacksonUtils;
import com.eagle.platform.api.constant.APIResultCodeEnum;



/**
 * 
 * 用户角色对应表 控制层
 * 
 */
@Controller
@RequestMapping("userRRoles")
public class UserRRolesController extends BaseController{

	@Resource
	private UserRRolesService userRRolesService;

	/**
	 * 查询所有用户角色或者单个用户角色信息
	 * param id (用户主键)
	 */
	@RequestMapping(value = "list", produces = "application/json;charset=UTF-8")
    @ResponseBody
	public String list(@RequestBody(required = false) String id) {
		List<UserRRoles> userRRoles=null;
		UserRRolesQuery query=null;
		if(!StringUtils.isAllBlank(id)) {
			System.out.println("id"+id);
			query=JacksonUtils.fromJson(id, UserRRolesQuery.class);
		}
		userRRoles=userRRolesService.findListByParam(query);
		if(query!=null) {
			query=new UserRRolesQuery();
		}
		return JacksonUtils.toJson(this.buildApiResult(APIResultCodeEnum.SUCCESS, userRRoles));
	}
	
	/**
	 * 添加用户角色信息
	 * param userRRoles (用户角色表)
	 * @throws BusinessException 
	 */
	@RequestMapping(value = "insert", produces = "application/json;charset=UTF-8")
    @ResponseBody
	public String insert(@RequestBody(required = false) UserRRoles userRRoles) throws BusinessException {
		Integer query = null;
		if(userRRoles!=null) {
			
			query=userRRolesService.add(userRRoles);
		}
		if (query == 1){
			return JacksonUtils.toJson(this.buildApiResult(APIResultCodeEnum.SUCCESS, null));
        }
		return JacksonUtils.toJson(this.buildApiResult(APIResultCodeEnum.ERROR, null));
	}
	
	
	/**
	 * 修改用户角色信息
	 * param userRRoles (用户角色表)
	 * param id (用户表主键)
	 * @throws BusinessException 
	 */
	@RequestMapping(value = "updateById", produces = "application/json;charset=UTF-8")
    @ResponseBody
	public String updateById(@RequestBody(required = false) UserRRoles userRRoles) throws BusinessException {
		Integer query = null;
		if(userRRoles!=null ) {
			Integer roleId=userRRoles.getRoleId();
			Integer userId=userRRoles.getUserId();
			query=userRRolesService.updateByUserIdAndRoleId(userRRoles, userId, roleId);
		}
		if (query == 1){
			return JacksonUtils.toJson(this.buildApiResult(APIResultCodeEnum.SUCCESS, null));
        }
		return JacksonUtils.toJson(this.buildApiResult(APIResultCodeEnum.ERROR, null));
	}
	
	/**
	 * 删除角色信息
	 * param id (用户表主键)
	 * @throws BusinessException 
	 */
	@RequestMapping(value = "deleteById", produces = "application/json;charset=UTF-8")
    @ResponseBody
	public String deleteById(@RequestBody(required = false)String id) throws BusinessException {
		Integer value = null;
		UserRRolesQuery query=null;
		if(id!=null) {
			query=JacksonUtils.fromJson(id, UserRRolesQuery.class);
			value=userRRolesService.deleteByUserIdAndRoleId(query.getUserId(), null);
		}
		if (value == 1){
			return JacksonUtils.toJson(this.buildApiResult(APIResultCodeEnum.SUCCESS, null));
        }
		return JacksonUtils.toJson(this.buildApiResult(APIResultCodeEnum.ERROR, null));
	}
}